package com.wenwei.sharebox.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.jakewharton.rxbinding.widget.RxTextView;
import com.wenwei.sharebox.R;
import com.wenwei.sharebox.base.BaseActivity;
import com.wenwei.sharebox.bean.AppointmentBean;
import com.wenwei.sharebox.net.ApiManager;
import com.wenwei.sharebox.net.BaseSubscriber;
import com.wenwei.sharebox.utils.AccountValidatorUtil;
import com.wenwei.sharebox.utils.UserStringUtils;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func2;
import rx.functions.Func4;

/**
 * Created by Administrator on 2017/7/10.
 */
public class UserAppointmentActivity extends BaseActivity {
    @Bind(R.id.user_name_ip)
    EditText userNameIp;
    @Bind(R.id.user_phone_ip)
    EditText userPhoneIp;
    @Bind(R.id.user_address_ip)
    EditText userAddressIp;
    @Bind(R.id.goods_name_ip)
    EditText goodsNameIp;
    @Bind(R.id.base_title)
    TextView baseTitle;
    @Bind(R.id.commit_btn)
    TextView commitBtn;

    @Override
    public int setLayoutResourceId() {
        return R.layout.activity_user_appointment;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    protected void initView() {
        super.initView();
        baseTitle.setText("我的预约");
        Observable<CharSequence> nameObservable = RxTextView.textChanges(userNameIp);
        Observable<CharSequence> phoneObservable = RxTextView.textChanges(userPhoneIp);
        Observable<CharSequence> addressObservable = RxTextView.textChanges(userAddressIp);
        Observable<CharSequence> goodsNameObservable = RxTextView.textChanges(goodsNameIp);
        Observable.combineLatest(nameObservable, phoneObservable,addressObservable,goodsNameObservable, new Func4<CharSequence, CharSequence, CharSequence, CharSequence, Boolean>() {
            @Override
            public Boolean call(CharSequence charSequence, CharSequence charSequence2
                    ,CharSequence charSequence3,CharSequence charSequence4) {
                return !charSequence.toString().equals("") && !charSequence2.toString().equals("")
                        && !charSequence3.toString().equals("")&& !charSequence4.toString().equals("")&& AccountValidatorUtil.isMobile(charSequence2.toString());
            }
        }).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean aBoolean) {
                commitBtn.setEnabled(aBoolean);
                if(aBoolean){
                    commitBtn.setBackgroundResource(R.drawable.bc_share_select);
                }else{
                    commitBtn.setBackgroundResource(R.drawable.bc_click_false);
                }
            }
        });
    }

    @OnClick({R.id.base_back, R.id.commit_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.base_back:
                finish();
                break;
            case R.id.commit_btn:
                commitAppointment();
                break;
        }
    }

    private BaseSubscriber<AppointmentBean> appointmentSub;
    private void commitAppointment(){
        appointmentSub = new BaseSubscriber<AppointmentBean>(mContext,true,"请稍后...") {
            @Override
            public void onSuccess(AppointmentBean appointmentBean) {
                showToast("预约成功，稍后工作人员会与您取得联系，请保持通讯畅通...");
                finish();
            }
        };
        try{
            JSONObject jb = new JSONObject();
            jb.put("detailedAddress",userAddressIp.getText().toString());
            jb.put("phone",userPhoneIp.getText().toString());
            jb.put("subscribeShopName",goodsNameIp.getText().toString());
            jb.put("userId", UserStringUtils.getUserId(mContext));
            jb.put("username",goodsNameIp.getText().toString());
            ApiManager.getInstance().userAppointment(appointmentSub,jb.toString());
        }catch (Exception e){}
    }
}
